package cn.lastcc.recover.contacts.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.lastcc.recover.contacts.android.R;
import cn.lastcc.recover.contacts.android.services.RecoverService;
import cn.lastcc.recover.contacts.android.utils.Constants;
import cn.lastcc.recover.contacts.android.utils.ServiceUtil;

public class MainActivity extends Activity {

    private Button btnStartService = null;
    private Button btnStopService =  null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        btnStartService = (Button) this.findViewById(R.id.btnStartService);
        btnStopService = (Button) this.findViewById(R.id.btnStopService);
        btnStartService.setOnClickListener(this.btnStartService_onClickListener);
        btnStopService.setOnClickListener(this.btnStopService_onClickListener);
        this.setBtnEnable();
        new Thread(this.monitorServiceStatus).start();
    }

    private void setBtnEnable() {
        if (ServiceUtil.isServiceRun(this, RecoverService.class.getName())) {
            btnStartService.setEnabled(false);
            btnStopService.setEnabled(true);
        } else {
            btnStartService.setEnabled(true);
            btnStopService.setEnabled(false);
        }
    }

    private OnClickListener btnStartService_onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(new Intent(MainActivity.this, RecoverService.class));
            setBtnEnable();
            Log.i(Constants.TAG, "RecoverService is started.");
        }
    };

    private OnClickListener btnStopService_onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(new Intent(MainActivity.this, RecoverService.class));
            setBtnEnable();
            Log.i(Constants.TAG, "RecoverService is stoped.");
        }
    };

    private static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int type = msg.arg1;
            switch (type) {
            case 0:
                MainActivity mainActivity = (MainActivity) msg.obj;
                mainActivity.setBtnEnable();
                break;
            }
        }
    };

    private Runnable monitorServiceStatus = new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Message msg = handler.obtainMessage();
                    msg.arg1 = 0;
                    msg.obj = MainActivity.this;
                    handler.sendMessage(msg);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

}
